<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:29
 */

class MarketService extends Service {

    public static function getEndDate($game){
        $g = PubPlatform::findGameByName($game);
        return  $g && $g['marketEndDate'] ? $g['marketEndDate'] : date('Y-m-d');
    }
    public static function levelHeader(){
        $header = array(
            'RecDate'   => array('text' => PubFunc::t('日期')),
            'SID'   => array('text' => PubFunc::t('服')),
            'RoleLevel'      => array('text' => PubFunc::t('等级'),'rule'=>'numeric'),
            'LiveUser'      => array('text' => PubFunc::t('订阅并创角数'),'rule'=>'numeric'),
            'NoRegButLiveUser'      => array('text' => PubFunc::t('未订阅的创角数'),'rule'=>'numeric'),
        );

        return $header;
    }

    public static function levelData($params){
        $data = ModMarket::levelData($params);
        return $data;
    }

    public static function addPercent($data){
        foreach($data as &$item){
            $all = $item["LiveUser"] + $item['NoRegButLiveUser'];
            $p1 = LibMath::getPercent($item['LiveUser'],$all);
            $p2 = LibMath::getPercent($item['NoRegButLiveUser'],$all);
            $item['LiveUser'] = $item['LiveUser'] . "(<span style='color:green;'>".$p1."%</span>)";
            $item['NoRegButLiveUser'] = $item['NoRegButLiveUser'] . "(<span style='color:green;'>".$p2."%</span>)";
        }
        unset($item);
        return $data;
    }
    public static function levelChart($data){
        $chartData = array();
        foreach($data as $v){
            $chartData[PubFunc::t("订阅并创角数")][$v['RoleLevel']] = $v["LiveUser"];
            $chartData[PubFunc::t("未订阅的创角数")][$v['RoleLevel']] = $v["NoRegButLiveUser"];
        }

        return array(
            "data"=> $chartData
        );
    }



    public static function detailHeader($params){

        $headers = array(
            "SubRegRole"=>array(
                'usermail'   => array('text' => PubFunc::t('用户邮箱')),
                'userid'   => array('text' => PubFunc::t('用户')),
                'subscribe_time'      => array('text' => PubFunc::t('订阅时间'),'rule'=>'numeric'),
                'ip'      => array('text' => PubFunc::t('注册IP'),'rule'=>'numeric'),
                'fromurl'      => array('text' => PubFunc::t('来源页')),
                'createtime'      => array('text' => PubFunc::t('注册时间')),
                'lastlogintime'  => array('text' => PubFunc::t('上次平台登录时间')),
                'game_lastlogintime'  => array('text' => PubFunc::t('上次游戏登录时间')),
                'country'   =>  array('text' => PubFunc::t('国家')),
                'fromdetail'   =>  array('text' => PubFunc::t('来源渠道')),
                'reg_fromurl'   =>  array('text' => PubFunc::t('注册来源页')),
                'level'   =>  array('text' => PubFunc::t('等级')),
                'username'   =>  array('text' => PubFunc::t('用户名')),
            ),
            "SubRegNoRole"  => array(
                'usermail'   => array('text' => PubFunc::t('用户邮箱')),
                'userid'   => array('text' => PubFunc::t('用户')),
                'subscribe_time'      => array('text' => PubFunc::t('订阅时间'),'rule'=>'numeric'),
                'ip'      => array('text' => PubFunc::t('注册IP'),'rule'=>'numeric'),
                'fromurl'      => array('text' => PubFunc::t('来源页')),
                'createtime'      => array('text' => PubFunc::t('注册时间')),
                'lastlogintime'  => array('text' => PubFunc::t('上次平台登录时间')),
                'game_lastlogintime'  => array('text' => PubFunc::t('上次游戏登录时间')),
                'country'   =>  array('text' => PubFunc::t('国家')),
                'fromdetail'   =>  array('text' => PubFunc::t('来源渠道')),
                'reg_fromurl'   =>  array('text' => PubFunc::t('注册来源页')),
                'level'   =>  array('text' => PubFunc::t('等级')),
                'username'   =>  array('text' => PubFunc::t('用户名')),
            ),
            "SubNoReg"  => array(
                'usermail'   => array('text' => PubFunc::t('用户邮箱')),
                'userid'   => array('text' => PubFunc::t('用户')),
                'subscribe_time'      => array('text' => PubFunc::t('订阅时间'),'rule'=>'numeric'),
                'ip'      => array('text' => PubFunc::t('注册IP'),'rule'=>'numeric'),
                'fromurl'      => array('text' => PubFunc::t('来源页')),
                'createtime'      => array('text' => PubFunc::t('注册时间')),
                'country'   =>  array('text' => PubFunc::t('国家')),
                'fromdetail'   =>  array('text' => PubFunc::t('来源渠道')),
                'reg_fromurl'   =>  array('text' => PubFunc::t('注册来源页')),
            ),
            "NoSubRegRole"  => array(
                'usermail'   => array('text' => PubFunc::t('用户邮箱')),
                'userid'   => array('text' => PubFunc::t('用户')),
                'nickyname'   => array('text' => PubFunc::t('用户昵称')),
                'facebookid'   => array('text' => PubFunc::t('FB账号ID')),
                'subscribe_time'      => array('text' => PubFunc::t('订阅时间'),'rule'=>'numeric'),
                'createtime'      => array('text' => PubFunc::t('注册时间')),
                'country'   =>  array('text' => PubFunc::t('国家')),
                'fromdetail'   =>  array('text' => PubFunc::t('来源渠道')),
                'regusermail'   =>  array('text' => PubFunc::t('注册邮箱')),
                'lastlogintime'  => array('text' => PubFunc::t('上次平台登录时间')),
                'game_lastlogintime'  => array('text' => PubFunc::t('上次游戏登录时间')),
            ),
            "NoSubRegNoRole"  =>  array(
                'usermail'   => array('text' => PubFunc::t('用户邮箱')),
                'userid'   => array('text' => PubFunc::t('用户')),
                'nickyname'   => array('text' => PubFunc::t('用户昵称')),
                'facebookid'   => array('text' => PubFunc::t('FB账号ID')),
                'subscribe_time'      => array('text' => PubFunc::t('订阅时间'),'rule'=>'numeric'),
                'createtime'      => array('text' => PubFunc::t('注册时间')),
                'lastlogintime'  => array('text' => PubFunc::t('上次平台登录时间')),
                'game_lastlogintime'  => array('text' => PubFunc::t('上次游戏登录时间')),
                'country'   =>  array('text' => PubFunc::t('国家')),
                'fromdetail'   =>  array('text' => PubFunc::t('来源渠道')),
                'regusermail'   =>  array('text' => PubFunc::t('注册邮箱')),

            ),
        );
        return $headers[$params['kpi']];
    }

    public static function detailData($params,$count = false){
        if($params['g'] !== 'st') return array();
        $data = ModMarket::detailData($params,$count);
        $result = PubStat::format($data,array("subscribe_time"=>'datetime',"createtime"=>'datetime','lastlogintime'=>'datetime','game_lastlogintime'=>'datetime'));

        foreach($result as &$v){
            $v['fromurl'] = "<a href='{$v['fromurl']}' target='_blank' style='display: inline-block;width:300px;word-break: keep-all;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;'>{$v['fromurl']}</a>";
            $v['reg_fromurl'] = "<a href='{$v['reg_fromurl']}' target='_blank' style='display: inline-block;width:300px;word-break: keep-all;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;'>{$v['reg_fromurl']}</a>";
        }
        unset($v);

        return $result;
    }
} 